//
//  MainTabBar.swift
//  wobo
//
//  Created by 杨博宇 on 16/3/6.
//  Copyright © 2016年 杨博宇. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        addSubview(plusBtn)
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义一个索引
        var index: CGFloat = 0
        
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                //修改subView的frame
                subView.frame = CGRectOffset(rect, index * w , 0)
                
                if index == 1 {
                    index++
                }
                
                index++
            }
        }
        
        addSubview(plusBtn)
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    //NSRK:懒加载
    lazy var plusBtn: UIButton = {
    
        //自定义样式的按钮
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        //自适应大小
        
        btn.sizeToFit()
        return btn
    }()
    
}
